<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class NoticeController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");

    public function index(){
		header('Content-Type:text/html;charset=utf-8');
             
		//-----------------搜索-------------------
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$where['title'] = array('LIKE','%'.$sh_q.'%');
		}
		

		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['addtime']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop_notice')->where($where)->order('add_time desc, id desc')->page($pageno.','.$pageCount)->select();
		$nowtime = time();
		foreach($list as $key=>$item){
		
                $list[$key]['shop_name'] = M('shop')->where('mid="'.$item['mid'].'"')->getField('shop_name');
	        if($item['end_time'] <= $nowtime ){ 
			    $list[$key]['check_status'] = 4; // 已过期
		 }	
			 
		}
             
	        $this->assign('list',$list);// 赋值数据集
		$count = M('shop')->where($where)->count();// 查询满足要求的总记录数
		$this->assign('count',$count);// 赋值分页输出	
		$Page = new \Think\Page($count,$pageCount);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->showAdmin();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出	
                $this->assign('statusArr',$this->statusArr);// 状态
		$this->display('Notice/index');
    }

  public  function opdown(){
	    $is_open = I('is_open');
	    $id = I('id');
		if(!empty( $is_open ) && !empty( $id )){
		
		   if( $is_open == 1){
		      $data['is_open'] = 2;
		      $resultA =  M('shop_notice')->where('id="'.$id.'"')->save($data);
		   } else{
		      $data['is_open'] = 1;
		      $resultB =  M('shop_notice')->where('id="'.$id.'"')->save($data);
		   }
		 if($resultA || $resultB){
          $this->redirect(U('Notice/index'));
          }
		}
     }
	
	public function editNotice(){
		header('Content-Type:text/html;charset=utf-8');
	        $id = I('id',0,'int');
		$dataInfo = M('shop_notice')->find($id);
		$dataInfo['shop_name'] = M('shop')->where('mid="'.$dataInfo['mid'].'"')->getField('shop_name');
		$this->assign('dataInfo', $dataInfo);
	        $this->display('Notice/edit');
    }	
	
	
	 public function checkStatus(){
	     $cid = I('cid');
	     $data['check_status'] = 2;
	     $result = M('shop_notice')->where('id="'.$cid .'"')->save($data);
	     if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	 }
	 
	  public function reject(){
	     $cid = I('cid');
	     $bh = I('bh');
	     $data['reject'] =  $bh;
	     $result = M('shop_notice')->where('id="'.$cid .'"')->save($data);
	     if( $result ){
		 $data = 1;
		 $this->ajaxReturn($data);
		 }
	 
	 }
	 
	 
	   public function savePoint(){
	     $cid = I('cid');
		 $data['point'] =  I('kd');
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $result = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->save($data);
		 if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	   }
}